import { ref } from 'vue'
import {
  queryDictByList,
  queryDictByTree,
  getDictValue,
  getDictStyle,
} from '@/utils/dict/index.js'

export const useDict = (keys = [], type = 'list') => {
  const initialization = () => {
    initListDictOptions(keys)
    if (type === 'list') {
      initListDictOptions(keys)
      return
    }
    initTreeDictOptions(keys)
  }

  // 字典项
  const dictOptions = ref({})

  // 初始化字典
  const initListDictOptions = (config) => {
    config.forEach((item) => {
      dictOptions.value[item] = queryDictByList(item)
    })
  }
  const initTreeDictOptions = (config) => {
    config.forEach((item) => {
      dictOptions.value[item] = queryDictByTree(item)
    })
  }

  initialization()

  return {
    dictOptions,
    get dict() {
      return dictOptions
    },
    initListDictOptions,
    initTreeDictOptions,
    getDictValue,
    getDictStyle,
  }
}
